package com.southpark.demo.view.popUps.userLogin
{
	import com.southpark.demo.control.events.info.AddInfoEvent;
	import com.southpark.demo.control.events.user.UserLoginEvent;
	import com.southpark.demo.enum.ServerErrorEnum;
	import com.southpark.demo.interfaces.popUps.IUserLoginPopUp;
	import com.southpark.demo.view.BaseViewController;
	import com.southpark.demo.view.popUps.userLogin.interfaces.IUserLoginPopUpCtrl;
	import com.southpark.demo.view.popUps.userLogin.interfaces.IUserLoginPopUpUI;
	import com.southpark.framework.control.commands.CommandFault;
	import com.southpark.framework.control.responders.IResponder;
	import com.southpark.framework.control.responders.ResponderFactory;
	
	
	public class UserLoginPopUpCtrl extends BaseViewController implements IUserLoginPopUp,IUserLoginPopUpCtrl
	{		
	
		public function UserLoginPopUpCtrl(view:IUserLoginPopUpUI)
		{
			super();
			this.view = view;
		}
		
		
		/**
		 * IViewController
		 */
		override public function onCreated():void
		{
			
		}
		
		override public function onShow():void
		{
			
		}
		
		override public function onHide():void
		{
			
		}
		
		
		/**
		 * IUserLoginPopUp
		 */
		public function setUserName(userName:String):void
		{
			view.setUserName(userName);
		}
		

		
		
		/**
		 * IUserLoginPopUpCtrl
		 */
		private var view:IUserLoginPopUpUI; 
		 
		public function userLogin(userName:String,password:String):void
		{
			var responder:IResponder = ResponderFactory.createResponder(onUserLoginSuccess,onUserLoginFault);
			var e:UserLoginEvent = new UserLoginEvent(UserLoginEvent.USER_LOGIN_EVENT,responder);
			e.userName = userName;
			e.password = password;
			e.rememberUser = true;
			
			e.dispatch();
		}
		
		private function onUserLoginSuccess(result:Object):void
		{
			var e:AddInfoEvent = new AddInfoEvent(AddInfoEvent.ADD_CONFIRMATION);
			e.text = applicationManager.textManager.getText("LOG_009");
			e.dispatch();
			
			view.removePopUp();
		}
		
		private function onUserLoginFault(commandFault:CommandFault):Boolean
		{
			if (commandFault.faultCode == ServerErrorEnum.LOGIN_ERROR.itsString)
			{
				var e:AddInfoEvent = new AddInfoEvent(AddInfoEvent.ADD_USER_ERROR);
				e.text = applicationManager.textManager.getText("LOG_008");
				e.dispatch();
				
				return true;
			};
			
			return false;
		}
		
	}
}